package com.shenhaofeng.app.widget.compat;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.TextView;


/**
 * TextView的一些兼容方法的补充
 * Created by Administrator on 2016/7/25.
 */
public class TextViewCompat {

    /**
     * 设置图片的渲染颜色
     * @param textView
     * @param color
     */
    public static void setDrawableTint(TextView textView, @ColorInt int color) {
        Drawable[] drawables = textView.getCompoundDrawables();
        for (int i = 0; i < drawables.length; i++) {
            Drawable drawable = drawables[i];
            if (drawable != null) {
                drawable = DrawableCompat.wrap(drawable);
                DrawableCompat.setTint(drawable, color);
                drawables[i] = drawable;
            }
        }
        textView.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
    }


    /**
     * 设置图片的渲染颜色资源
     * @param textView
     * @param colorRes
     */
    public static void setDrawableTintRes(TextView textView, @ColorRes int colorRes) {
        Drawable[] drawables = textView.getCompoundDrawables();
        int color = textView.getContext().getResources().getColor(colorRes);
        for (int i = 0; i < drawables.length; i++) {
            Drawable drawable = drawables[i];
            if (drawable != null) {
                drawable = DrawableCompat.wrap(drawable);
                DrawableCompat.setTint(drawable, color);
                drawables[i] = drawable;
            }
        }
        textView.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
    }





    public static void setDrawableTintList(TextView textView, @ColorInt ColorStateList colorStateList) {
        Drawable[] drawables = textView.getCompoundDrawables();
        for (int i = 0; i < drawables.length; i++) {
            Drawable drawable = drawables[i];
            if (drawable != null) {
                drawable = DrawableCompat.wrap(drawable);
                DrawableCompat.setTintList(drawable, colorStateList);
                drawables[i] = drawable;
            }
        }
        textView.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
    }





    public static void setDrawableTintList(TextView textView, @ColorRes int colorStateListRes) {
        Drawable[] drawables = textView.getCompoundDrawables();
        ColorStateList colorStateList = textView.getContext().getResources().getColorStateList(colorStateListRes);
        for (int i = 0; i < drawables.length; i++) {
            Drawable drawable = drawables[i];
            if (drawable != null) {
                drawable = DrawableCompat.wrap(drawable);
                DrawableCompat.setTintList(drawable, colorStateList);
                drawables[i] = drawable;
            }
        }
        textView.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
    }


}
